const router = require('koa-router')();
const { findNoteListByType, findNoteDetailById, publishNote } = require('../controllers/index.js')
const jwt = require('../utils/jwt.js')

router.get('/findNoteListByType', jwt.verify(), async (ctx) => {
    //获取前端传递的 note_type, 去数据库中以改note_type字段读取数据，返回给前端
    const { note_type } = ctx.request.query
    // console.log(ctx.userId, '------');
    // console.log(note_type);
    try {
        const res = await findNoteListByType(note_type, ctx.userId)
        if (res.length) {
            ctx.body = {
                code: '8000',
                data: res,
                msg: '查询成功'
            }
        } else {
            ctx.body = {
                code: '8000',
                data: 'success',
                msg: '当前分类下没有数据'
            }
        }
    } catch (error) {
        ctx.body = {
            code: '8005',
            data: error, //不用打引号，因为error是object
            msg: '服务器异常'
        }
    }
})


router.get('/findNoteDetailById', jwt.verify(), async (ctx) => {
    const { id } = ctx.request.query
    try {
        const res = await findNoteDetailById(id)
        console.log(res);
        if (res.length) {
            ctx.body = {
                code: '8000',
                data: res,
                msg: '查询成功'
            }
            return
        } else {
            ctx.body = {
                code: '8000',
                data: 'error',
                msg: '查询失败'
            }
        }
    } catch (error) {
        ctx.body = {
            code: '8000',
            data: error,
            msg: '服务器异常'
        }
    }
})

router.post(('/note-publish'), jwt.verify(), async (ctx) => {
    console.log(ctx.request.body);
    try {
        const res = await publishNote(ctx.request.body)
        if (res) {
            ctx.body = {
                code: '8000',
                msg: '日记发布成功',
                date: res
            }
        } else {
            ctx.body = {
                code: '8000',
                msg: '日记发布失败'
            }
        }
    } catch (err) {
        console.log(err);
        ctx.body = {
            code: '8001',
            msg: '服务器异常,日记发布失败',
            data: err
        }
    }
})



module.exports = router